package com.aaa.web.controller.appstorecontroller;

import com.aaa.entity.appstore.ElectromobileMessage;
import com.aaa.util.PictureUtil;
import com.aaa.web.commons.Result;
import com.aaa.web.commons.SecondCarEntrepotQuery;
import com.aaa.web.service.appstoreservice.ElectromobilePartsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program:
 * @description: 配件商城页面
 * @author: 许一鑫
 * @create:
 **/
@Controller
@RequestMapping("/ElectromobilePartsController")
public class ElectromobilePartsController {
    /**
     * 自动注入service
     */
    @Autowired
    private ElectromobilePartsService service;

    /**
     * 跳转配件商品界面
     * @return
     */
    @RequestMapping("/GetElectromobilePartsPage")
    public String getElectromobilePartsPage(){
        return "views/webhtml/appstore/electromobileparts";
    }

    /**
     * 获取所有配件商品信息,分页，搜索框
     * @param query
     * @return
     */
    @RequestMapping("GetElectromobilePartsMessage")
    @ResponseBody
    public Map getElectromobilePartsMessage(SecondCarEntrepotQuery query){
        return service.getElectromobilePartsMessage(query);
    }

    /**
     * 根据商品名下架已有的商品
     * @param goodsname
     * @return
     */
    @RequestMapping("/DelGoods")
    @ResponseBody
    public Result delGoods(String goodsname){
        return service.delGoods(goodsname);

    }

    /**
     * 上传照片
     * @param file
     * @return
     */
    @RequestMapping("/uploadPhoto")
    @ResponseBody
    public Map uploadPhoto(@Param("file") MultipartFile file){
        File f = null;
        try {
            f= File.createTempFile("tmp", null);
            file.transferTo(f);
            f.deleteOnExit();
            String imgurl = PictureUtil.getPicture(f);
            Map<String, Object> map = new HashMap<>();
            map.put("data",imgurl);
            map.put("code",0);
            map.put("msg","");
            return  map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 添加新商品
     * @param e
     * @return
     */
    @RequestMapping("/addNewGoods")
    @ResponseBody
    public Result addNewGoods(ElectromobileMessage e){
        return service.addNewGoods(e);
    }
}
